extends Node2D

const MINIMAP_SIZE = Vector2(80, 80)
const ROOM_SIZE = 3
const CONNECTION_WIDTH = 1
const COLORS = {
	FIGHT = Color.WHITE,
	REWARD = Color.ROYAL_BLUE,
	STORE = Color.LIME_GREEN,
	BOSS = Color.CRIMSON,
	CURRENT = Color.GOLD
}

enum {TOP = 0, RIGHT = 1, BOTTOM = 2, LEFT = 3}

var existRoom = [Vector2.ZERO]
var mapConfig = []
var SCREEN_SIZE = Vector2(480, 272)
var minimap_rect: Rect2
var current_room_pos = Vector2.ZERO

@onready var util = Util.new()

func _init():
	self.mapConfig = GenerateMap.new().generate_map_exsit_boss()
	
func _ready():
	init_minimap()
	update_minimap(Vector2.ZERO)

func init_minimap():
	# 清空房间实体
	var roomObjs = util.findOne("$[Rooms]", get_tree().root.get_child(0))
	for child in roomObjs.get_children():
		child.queue_free()
	
	# 清空房间标记
	existRoom = [Vector2.ZERO]
	
	minimap_rect = Rect2(
		get_viewport_rect().size.x - MINIMAP_SIZE.x - 20,
		20,
		MINIMAP_SIZE.x,
		MINIMAP_SIZE.y
	)

func _draw():
	draw_minimap()

func draw_minimap():
	# 背景
	draw_rect(minimap_rect, Color(1, 1, 1, 0.3), 1.5)
	
	# 计算世界边界
	var bounds = calculate_world_bounds()
	var scale_factor = Vector2(
		minimap_rect.size.x / bounds.size.x,
		minimap_rect.size.y / bounds.size.y
	)
	
	# 绘制连接线
	for room in mapConfig:
		var from_pos = world_to_minimap(room.position, bounds, scale_factor)
		for dir in room.door:
			var neighbor_pos = room.position + direction_to_vector(dir)
			if has_room_at(neighbor_pos):
				var to_pos = world_to_minimap(neighbor_pos, bounds, scale_factor)
				draw_line(from_pos, to_pos, Color.WHITE, CONNECTION_WIDTH)
	
	# 绘制房间
	for room in mapConfig:
		var pos = world_to_minimap(room.position, bounds, scale_factor)
		var color = COLORS.values()[room.type]
		if room.position == current_room_pos:
			color = COLORS.CURRENT
		draw_room(pos, color)

func draw_room(position, color):
	var rect = Rect2(
		position - Vector2.ONE * ROOM_SIZE/2,
		Vector2.ONE * ROOM_SIZE
	)
	draw_rect(rect, color, true)
	draw_rect(rect, Color.BLACK, false, 1)

func calculate_world_bounds():
	var min_pos = Vector2.INF
	var max_pos = -Vector2.INF
	for room in mapConfig:
		min_pos = Vector2(
			min(min_pos.x, room.position.x),
			min(min_pos.y, room.position.y)
		)
		max_pos = Vector2(
			max(max_pos.x, room.position.x),
			max(max_pos.y, room.position.y)
		)
	return Rect2(min_pos, max_pos - min_pos + SCREEN_SIZE)

func world_to_minimap(world_pos, bounds, scale):
	var relative_pos = world_pos - bounds.position
	return Vector2(
		minimap_rect.position.x + relative_pos.x * scale.x,
		minimap_rect.position.y + relative_pos.y * scale.y
	)

func has_room_at(position):
	for room in mapConfig:
		if room.position == position:
			return true
	return false

func direction_to_vector(dir):
	match dir:
		TOP: return Vector2(0, -SCREEN_SIZE.y)
		RIGHT: return Vector2(SCREEN_SIZE.x, 0)
		BOTTOM: return Vector2(0, SCREEN_SIZE.y)
		LEFT: return Vector2(-SCREEN_SIZE.x, 0)
	return Vector2.ZERO

func update_minimap(new_position):
	current_room_pos = new_position
	queue_redraw()
